//
//  CZSettingItemCell.m
//  0908网易彩票01
//
//  Created by Apple on 15/9/8.
//  Copyright (c) 2015年 itcast. All rights reserved.
//

#import "ZDSettingItemCell.h"
#import "ZDSettingItem.h"
#import "ZDSettingItemArrow.h"
#import "ZDSettingItemSwitch.h"
#import "ZDSettingItemLabel.h"
#import "ZDAccessoryButton.h"


@interface ZDSettingItemCell ()

@property (nonatomic, strong) UIView *lineView;

@property (nonatomic, strong) UIImageView *arrow;

@property (nonatomic, strong) UISwitch *sw;

@property (nonatomic, strong) UILabel *label;

@property (nonatomic, strong) ZDAccessoryButton *accessBtn;

@end

@implementation ZDSettingItemCell

- (UIView *)lineView
{
    if (_lineView == nil)
    {
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = [UIColor blackColor];
        _lineView.alpha = 0.2;
    }
    
    return _lineView;
}

//懒加载
- (UIImageView *)arrow
{
    if (_arrow == nil)
    {
        _arrow = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"CellDisclosureIndicator"]];
    }
    
    return _arrow;
}

- (UISwitch *)sw
{
    if (_sw == nil)
    {
        _sw = [[UISwitch alloc] init];
        
        //为开关添加绑定事件，监听开关状态的改变
        [_sw addTarget:self action:@selector(switchValueChanged) forControlEvents:UIControlEventValueChanged];
    }
    
    return _sw;
}

- (UILabel *)label
{
    if (_label == nil)
    {
        _label = [[UILabel alloc] initWithFrame:CGRectMake(200, 10, 200, 20)];
        
//        _label.backgroundColor = [UIColor orangeColor];
    }
    
    return _label;
}

- (ZDAccessoryButton *)accessBtn
{
    if (!_accessBtn)
    {
        _accessBtn = [[ZDAccessoryButton alloc] initWithFrame:CGRectMake(200, 10, 150, 20)];
        
        [_accessBtn setImage:[UIImage imageNamed:@"CellDisclosureIndicator"] forState:UIControlStateNormal];
//        _accessBtn.backgroundColor = [UIColor orangeColor];
        [_accessBtn setTitleColor:ZDSameColor(92) forState:UIControlStateNormal];
        _accessBtn.userInteractionEnabled = NO;
    }
    
    return _accessBtn;
}

//监听开关状态的改变
- (void)switchValueChanged
{
#warning 这里选择在开关状态改变的时候保存开关状态
    //保存开关状态
    ZDSettingItemSwitch *item = (ZDSettingItemSwitch *)self.item;
    item.on = self.sw.on;
    
    //如果有定义block，就调用block
    if (item.switchBlock)
    {
        item.switchBlock(self.sw.on);
    }
}

+ (instancetype)settingItemCell:(UITableView *)tableView
{
    static NSString *ID = @"ZDSettingItemCell";
    
    ZDSettingItemCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (cell == nil)
    {
        cell = [[ZDSettingItemCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    
    return cell;
}


//重写构造方法
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        //设置cell选中的背景颜色
        UIView *backgroundView = [[UIView alloc] init];
        backgroundView.backgroundColor = ZDColor(227, 221, 221);
        self.selectedBackgroundView = backgroundView;
        
        //设置文本的颜色
        self.textLabel.textColor = ZDSameColor(92);
        self.detailTextLabel.textColor = ZDColor(136, 101, 101);
        
        //设置文本的字体
        self.textLabel.font = ZDFont(15);
        self.detailTextLabel.font = ZDFont(13);
        
        //设置时间右对齐
        self.label.textAlignment = NSTextAlignmentRight;
        self.label.textColor = ZDColor(253, 169, 55);
        
        [self.contentView addSubview:self.lineView];
    }
    
    return self;
}

- (void)setItem:(ZDSettingItem *)item
{
    _item = item;
    
    self.textLabel.text = item.title;
    self.imageView.image = item.icon ? [UIImage imageNamed:item.icon] : nil;
    self.detailTextLabel.text = item.detailText;
    
    if ([item isKindOfClass:[ZDSettingItemArrow class]])
    {
        [self.accessBtn setTitle:[(ZDSettingItemArrow *)item accessLabel] forState:UIControlStateNormal];
        
        self.accessoryView = self.accessBtn;
    
//        self.accessoryView = self.arrow;
    }
    else if ([item isKindOfClass:[ZDSettingItemSwitch class]])
    {
        self.accessoryView = self.sw;
        //读取开关状态
        self.sw.on = [(ZDSettingItemSwitch *)item on];
    
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    else if ([item isKindOfClass:[ZDSettingItemLabel class]])
    {
        self.label.text = [(ZDSettingItemLabel *)item label];
        self.accessoryView = self.label;
    }
}

- (void)setShowBottomLine:(BOOL)showBottomLine
{
    _showBottomLine = showBottomLine;
    
    self.lineView.hidden = _showBottomLine ? NO : YES;      //关闭隐藏分割线
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat lineX = self.textLabel.frame.origin.x;
    CGFloat lineW = self.frame.size.width - lineX;
    CGFloat lineH = 1;
    CGFloat lineY = self.frame.size.height - lineH;
    
    self.lineView.frame = CGRectMake(lineX, lineY, lineW, lineH);
}

@end
